/****************************************************
 * 													*
 * 					BanzaiP2P						*
 * 													*
 * Versione: 0.1.1									*
 * Autore: D'Annessa Marco							*
 * File: config_loader.c							*
 ****************************************************/


#include "config_loader.h"


struct config*  config_loader(char* config_file){


	FILE *cs;
	char line[255];
	char *set="=";
	char *token;
	int k;
	
	struct config *temp;
	temp = (struct config*)malloc(sizeof(struct config));
	
	cs = fopen(config_file,"rb");
	
	if(cs == 0){
		
		printf("File di configurazione non trovato\n");
		
		//dafault settings
		temp->sharefolder = (char*)malloc(6);
		strcpy(temp->sharefolder,"/share");
		temp->downfolder = (char*)malloc(9);
		strcpy(temp->downfolder,"/download");
		temp->nick = (char*)malloc(10);
		strcpy(temp->nick,"anonimous");
		temp->port = 2345;
		
		printf("Configurazione di default caricata\n");
		
	}
	
	else{	
		
		fgets(line,255,cs);
		token = strtok(line,set);
		token = strtok(NULL,set);
		temp->sharefolder = (char*)malloc(strlen(token));
		for(k=0;k<strlen(token)-1;k++)
			temp->sharefolder[k]=token[k];
	
		fgets(line,255,cs);
		token = strtok(line,set);
		token = strtok(NULL,set);
		temp->downfolder = (char*)malloc(strlen(token));
		for(k=0;k<strlen(token)-1;k++)
			temp->downfolder[k]=token[k];
		
		fgets(line,255,cs);
		token = strtok(line,set);
		token = strtok(NULL,set);
		temp->port = atoi(token);
		
		fgets(line,255,cs);
		token = strtok(line,set);
		token = strtok(NULL,set);
		temp->bootfile = (char*)malloc(strlen(token));
		for(k=0;k<strlen(token)-1;k++)
			temp->bootfile[k]=token[k];
		
		fgets(line,255,cs);
		token = strtok(line,set);
		token = strtok(NULL,set);
		temp->bootpeernumber = atoi(token);
		
		fgets(line,255,cs);
		token = strtok(line,set);
		token = strtok(NULL,set);
		temp->nick = (char*)malloc(strlen(token));
		for(k=0;k<strlen(token)-1;k++)
			temp->nick[k]=token[k];
		
		fgets(line,255,cs);
		token = strtok(line,set);
		token = strtok(NULL,set);
		temp->maxcon_incoming = atoi(token);

		
		printf("File di configurazione caricato\n");
	}

	fclose(cs);

return temp ;
}



